package com.nari.ies.ne.nw.server.service.fileQuality.impl;

import com.nari.ies.ne.nw.server.entity.FileQualityEntity;
import com.nari.ies.ne.nw.server.service.fileQuality.IQXHJFileQualityService;
import com.nari.ies.ne.nw.server.util.DateUtil;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Date;
@Slf4j
@Service
public class QXHJFileQualityServiceImpl implements IQXHJFileQualityService {

    @Override
    public FileQualityEntity FdQXHJFileQuality(File file, FileQualityEntity fileQualityEntity) {
        // 文件是否延时
        int fileDelay = 0;
        // 文件内容是否为空
        int empty = 0;
        // 文件标签是否缺失
        int missingTag = 0;
        // 文件内容是否缺失
        int defect = 0;
        //文件格式是否有误（数据列分割后数目不对）
        int fileFormatError = 0;
        //文件内容格式是否有误（数据列类型转换报错）
        int fileContentError = 0;

        // 信息
        String errorInfo = fileQualityEntity.getMessage();
        //总体信息内容缺失
        int ztxxDefect = 0;
        //气象环境内容缺失
        int qxhjDefect = 0;

        //逆变器标题长度
        int ztxxCount = 0;
        //汇流箱标题长度
        int qxhjCount = 0;

        // 判断时间是否延时
        Date date = new Date(file.lastModified());
        fileDelay = DateUtil.getBetweenMinute(date, fileQualityEntity.getForecastDate());
        if (fileDelay == 0) {
            errorInfo = "文件延时";
        }

        // 判断内容是否为空
        if (file.length() == 0) {
            errorInfo = "文件内容为空";

            fileQualityEntity.setIsFileDelay(fileDelay);
            fileQualityEntity.setIsFileEmpty(empty);
            fileQualityEntity.setIsFileDefect(defect);
            fileQualityEntity.setIsFileMissTag(missingTag);
            fileQualityEntity.setIsFileFormatError(fileFormatError);
            fileQualityEntity.setIsFileContentError(fileContentError);
            fileQualityEntity.setMessage(errorInfo);

            return fileQualityEntity;
        } else {
            empty = 1;
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(file);
                br = new BufferedReader(fr);

                // 每行数据
                String len = "";
                // 记录标签行数
                int labelCount = 0;

                // 标签字符拼接
                String[] fileType = fileQualityEntity.getFileType().split("-");
                String plantCode = fileQualityEntity.getPlantCode();

                String ZTXXFirstLabel = "<" + fileType[0] + "::" + plantCode;
                String ZTXXEndLabel = "</" + fileType[0] + "::" + plantCode + ">";
                String QXHJFirstLabel = "<" + fileType[1] + "::" + plantCode;
                String QXHJEndLabel = "</" + fileType[1] + "::" + plantCode + ">";
                while ((len = br.readLine()) != null) {
                    if (len.length() == 0) {
                        continue;
                    }
                    // 分割字符串按照tab标签
                    String[] strs = len.split("\t");
                    // 判断标签是否缺失不符合规范
                    if (len.contains("<")) {
                        if (1 == labelCount) {
                            //总体信息
                            if (!(ZTXXFirstLabel.equals(strs[0]))) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }
                        if (2 == labelCount) {
                            //总体信息
                            if (!(ZTXXEndLabel.equals(strs[0]))) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }

                        if (3 == labelCount) {
                            //气象环境
                            if (!QXHJFirstLabel.equals(strs[0])) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }
                        if (4 == labelCount) {
                            //气象环境
                            if (!QXHJEndLabel.equals(strs[0])) {
                                errorInfo = "文件标签缺失";
                                break;
                            } else {
                                missingTag = 1;
                            }
                        }
                        labelCount++;
                    }

                    if (labelCount == 2) {
                        //总体信息
                        if (len.contains("@")) {
                            ztxxCount = strs.length;
                        }
                        if (len.contains("#")) {
                            ztxxDefect = 1;
                            if (ztxxCount != strs.length) {
                                errorInfo = " 文件格式错误";
                            } else {
                                for (int i = 4; i < strs.length - 1; i++) {
                                    if (!"null".equals(strs[i]) && !"NULL".equals(strs[i]) && !" ".equals(strs[i])) {
                                        Float.parseFloat(strs[i]);
                                    }
                                }
                            }
                        }
                    }

                    if (labelCount == 4) {
                        //气象环境
                        if (len.contains("@")) {
                            qxhjCount = strs.length;
                        }
                        // 判断内容是否缺失
                        if (len.contains("#")) {
                            qxhjDefect = 1;
                            //判断文件格式是否有误
                            if (qxhjCount != strs.length) {
                                errorInfo = "文件格式错误";
                                break;
                            } else {
                                fileFormatError = 1;
                                for (int i = 2; i < strs.length; i++) {
                                    if (!"null".equals(strs[i]) && !"NULL".equals(strs[i]) && !" ".equals(strs[i])) {
                                        Float.parseFloat(strs[i]);
                                    }
                                }
                                fileContentError = 1;
                            }
                        }
                    }
                }

                if (ztxxDefect != 1) {
                    if (!errorInfo.equals("文件标签缺失".trim())) {
                        errorInfo = "文件总体信息内容缺失";
                    }
                } else if (qxhjDefect != 1) {
                    if (!errorInfo.equals("文件标签缺失".trim())) {
                        errorInfo = "文件气象环境内容缺失";
                    }
                } else {
                    defect = 1;
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                errorInfo = "文件内容格式错误";
                e.printStackTrace();
            } finally {

                IoUtil.close(fr);
                IoUtil.close(br);

                fileQualityEntity.setIsFileDelay(fileDelay);
                fileQualityEntity.setIsFileEmpty(empty);
                fileQualityEntity.setIsFileDefect(defect);
                fileQualityEntity.setIsFileMissTag(missingTag);
                fileQualityEntity.setIsFileFormatError(fileFormatError);
                fileQualityEntity.setIsFileContentError(fileContentError);
                fileQualityEntity.setMessage(errorInfo);

                return fileQualityEntity;
            }
        }
    }


    @Override
    public FileQualityEntity GfQXHJFileQuality(File file, FileQualityEntity fileQualityEntity) {
        // 文件是否延时
        int fileDelay = 0;
        // 文件内容是否为空
        int empty = 0;
        // 文件标签是否缺失
        int missingTag = 0;
        // 文件内容是否缺失
        int defect = 0;
        //文件格式是否有误（数据列分割后数目不对）
        int fileFormatError = 0;
        //文件内容格式是否有误（数据列类型转换报错）
        int fileContentError = 1;

        // 信息
        String errorInfo = fileQualityEntity.getMessage();
        //总体信息内容缺失
        int ztxxDefect = 0;
        //气象环境内容缺失
        int qxhjDefect = 0;
        //太阳跟踪内容缺失
        int tygzDefect = 0;

        //逆变器标题长度
        int ztxxCount = 0;
        //汇流箱标题长度
        int qxhjCount = 0;
        //太阳跟踪标题长度
        int tygzCount = 0;

        // 判断时间是否延时
        Date date = new Date(file.lastModified());
        fileDelay = DateUtil.getBetweenMinute(date, fileQualityEntity.getForecastDate());
        if (fileDelay == 0) {
            errorInfo = "文件延时";
        }

        // 判断内容是否为空
        if (file.length() == 0) {
            errorInfo = "文件内容为空";

            fileQualityEntity.setIsFileDelay(fileDelay);
            fileQualityEntity.setIsFileEmpty(empty);
            fileQualityEntity.setIsFileDefect(defect);
            fileQualityEntity.setIsFileMissTag(missingTag);
            fileQualityEntity.setIsFileFormatError(fileFormatError);
            fileQualityEntity.setIsFileContentError(fileContentError);
            fileQualityEntity.setMessage(errorInfo);

            return fileQualityEntity;
        } else {
            empty = 1;
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(file);
                br = new BufferedReader(fr);

                // 每行数据
                String len = "";
                // 记录标签行数
                int labelCount = 0;

                // 标签字符拼接
                String[] fileType = fileQualityEntity.getFileType().split("-");
                String plantCode = fileQualityEntity.getPlantCode();

                String ZTXXFirstLabel = "<" + fileType[0] + "::" + plantCode;
                String ZTXXEndLabel = "</" + fileType[0] + "::" + plantCode + ">";
                String QXHJFirstLabel = "<" + fileType[1] + "::" + plantCode;
                String QXHJEndLabel = "</" + fileType[1] + "::" + plantCode + ">";
                String TYGZFirstLabel = "<" + fileType[2] + "::" + plantCode;
                String TYGZEndLabel = "</" + fileType[2] + "::" + plantCode + ">";

                while ((len = br.readLine()) != null) {
                    if (len.length() == 0) {
                        continue;
                    }

                    String[] strs = len.split("\t");
                    // 判断标签是否缺失不符合规范
                    if (len.contains("<")) {
                        if (1 == labelCount) {
                            //总体信息
                            if (!(ZTXXFirstLabel.equals(strs[0]))) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }
                        if (2 == labelCount) {
                            //总体信息
                            if (!(ZTXXEndLabel.equals(strs[0]))) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }

                        if (3 == labelCount) {
                            //气象环境
                            if (!QXHJFirstLabel.equals(strs[0])) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }
                        if (4 == labelCount) {
                            //气象环境
                            if (!QXHJEndLabel.equals(strs[0])) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }

                        if (5 == labelCount) {
                            //太阳跟踪
                            if (!TYGZFirstLabel.equals(strs[0])) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }

                        if (6 == labelCount) {
                            //太阳跟踪
                            if (!TYGZEndLabel.equals(strs[0])) {
                                errorInfo = "文件标签缺失";
                                break;
                            } else {
                                missingTag = 1;
                            }
                        }

                        labelCount++;
                    }

                    if (labelCount == 2) {
                        //总体信息
                        if (len.contains("@")) {
                            ztxxCount = strs.length;
                        }
                        if (len.contains("#")) {
                            ztxxDefect = 1;
                            if (ztxxCount != strs.length) {
                                errorInfo = "文件格式有误";
                            } else {
                                for (int i = 4; i < strs.length - 1; i++) {
                                    if (!"null".equals(strs[i]) && !"NULL".equals(strs[i]) && !" ".equals(strs[i])) {
                                        Float.parseFloat(strs[i]);
                                    }
                                }
                            }
                        }
                    }

                    if (labelCount == 4) {
                        //气象环境
                        if (len.contains("@")) {
                            qxhjCount = strs.length;
                        }
                        // 判断内容是否缺失
                        if (len.contains("#")) {
                            qxhjDefect = 1;
                            //判断文件格式是否有误
                            if (qxhjCount != strs.length) {
                                errorInfo = "文件格式有误";
                            } else {
                                for (int i = 2; i < strs.length; i++) {
                                    if (!"null".equals(strs[i]) && !"NULL".equals(strs[i]) && !" ".equals(strs[i])) {
                                        Float.parseFloat(strs[i]);
                                    }
                                }
                            }
                        }
                    }

                    if (labelCount == 6) {
                        //太阳跟踪
                        if (len.contains("@")) {
                            tygzCount = strs.length;
                        }
                        // 判断内容是否缺失
                        if (len.contains("#")) {
                            tygzDefect = 1;
                            //判断文件格式是否有误
                            if (tygzCount != strs.length) {
                                errorInfo = "文件格式有误";
                                break;
                            } else {
                                fileFormatError = 1;
                                for (int i = 2; i < strs.length; i++) {
                                    if (!"null".equals(strs[i]) && !"NULL".equals(strs[i]) && !" ".equals(strs[i])) {
                                        Float.parseFloat(strs[i]);
                                    }
                                }
                            }
                        }
                    }
                }

                if (ztxxDefect != 1) {
                    if (!errorInfo.equals("文件标签缺失".trim())) {
                        errorInfo = "文件总体信息内容缺失";
                    }
                } else if (qxhjDefect != 1) {
                    if (!errorInfo.equals("文件标签缺失".trim())) {
                        errorInfo = "文件气象环境内容缺失";
                    }
                } else if (tygzDefect != 1) {
                    if (!errorInfo.equals("文件标签缺失".trim())) {
                        errorInfo = "文件太阳跟踪内容缺失";
                    }
                } else {
                    defect = 1;
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                fileContentError = 0;
                errorInfo = "文件内容格式有误";
                e.printStackTrace();
            } finally {

                IoUtil.close(fr);
                IoUtil.close(br);

                fileQualityEntity.setIsFileDelay(fileDelay);
                fileQualityEntity.setIsFileEmpty(empty);
                fileQualityEntity.setIsFileDefect(defect);
                fileQualityEntity.setIsFileMissTag(missingTag);
                fileQualityEntity.setIsFileFormatError(fileFormatError);
                fileQualityEntity.setIsFileContentError(fileContentError);
                fileQualityEntity.setMessage(errorInfo);

                return fileQualityEntity;
            }
        }
    }
}
